/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.jSkyRoom.generator.command.rule;

import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Rakev
 */
public class CollectionAdapterRule<E> implements Rule<Collection<E>> {

    private Rule<E> elementRule;

    public CollectionAdapterRule(Rule<E> elementRule) {
        this.elementRule = elementRule;
    }

    @Override
    public boolean check(Collection<E> items) {
        for (E item : items) {
            if (!elementRule.check(item)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Collection<E> apply(Collection<E> items) {
        Collection<E> resultItems = new ArrayList<>(items.size());
        for (E item : items) {
            resultItems.add(elementRule.apply(item));
        }
        return resultItems;
    }
}
